﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.Data;
using Telerik.Web.UI;

namespace MLM.AdminCP.Controls
{
    public partial class LanguageSelect : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LanguageSelectCombobox.Label = Resources.MLM.SelectLanguage;
            if (!IsPostBack)
            {
                MLMDataContext ct = new MLMDataContext();
                var languages = ct.Languages.OrderBy(it=>it.Name);
                foreach (var language in languages)
                {
                    var item = new RadComboBoxItem(language.Name, language.LanguageCode);
                    item.ImageUrl = "~/AdminCP/LanguageIcon/24/" + language.FlagIconName + ".png";
                    LanguageSelectCombobox.Items.Add(item);
                    LanguageSelectCombobox.SelectedValue = "de-DE";
                }
                if (Session["MLMCulture"] != null)
                {
                    LanguageSelectCombobox.SelectedValue = Session["MLMCulture"].ToString();
                }                
            }
            LanguageSelectCombobox.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(LanguageSelectCombobox_SelectedIndexChanged);
        }

        void LanguageSelectCombobox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["MLMCulture"] = e.Value;
            Response.Redirect(Request.RawUrl);
        }
    }
}